package socket;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 客户端
 */
public class Client {
    private Socket socket;

    public Client() {
        try {
            System.out.println("客户端启动...");
            socket = new Socket("176.55.10.16", 8088);
            System.out.println("客户端启动成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void start() {
        try {
            //通过socket获取输出流用于向服务端发送数据
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw, true);

            Scanner sc= new Scanner(System.in);
            while (true) {
                System.out.println("请输入内容:");
                String str = sc.nextLine();
                if ("exit".equalsIgnoreCase(str)) {
                    break;
                }
                pw.println(str);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();//进行四次挥手
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }

}
